﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

public partial class MasterPage_MasterPage1 : System.Web.UI.MasterPage
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (CheckAdmin())
        {
            hplAdmin.Visible = true;
        }
        else
        {
            hplAdmin.Visible = false;
        }
        if (Session.Contents["UserEmail"] == null & Request.Cookies["UserEmail"] == null)
        {
            MultiView1.ActiveViewIndex = 0;
        }
        if (Session.Contents["UserEmail"] == null & Request.Cookies["UserEmail"] != null)
        {
            if (Request.Cookies["UserEmail"].Value == "")
                MultiView1.ActiveViewIndex = 0;
            else
            {
                MultiView1.ActiveViewIndex = 1;
                GetAvatar();
            }
        }
        if (Session.Contents["UserEmail"] != null & Request.Cookies["UserEmail"] != null)
        {
            MultiView1.ActiveViewIndex = 1;
            GetAvatar();
        }
        if (Session.Contents["UserEmail"] != null & Request.Cookies["UserEmail"] == null)
        {
            MultiView1.ActiveViewIndex = 1;
            GetAvatar();
        }
    }
    protected void LinkButton1_Click(object sender, EventArgs e)
    {
        Session.Abandon();
        if (Request.Cookies["UserEmail"].Value != null)
        {
            Response.Cookies["UserEmail"].Expires = DateTime.Now.AddMonths(-1);
            Response.Cookies.Remove("UserEmail");
        }
        Response.Redirect("~/Default.aspx");
        
    }
    protected void GetAvatar()
    {
        SQLHelper Dal = new SQLHelper();
        Dal.CommandType = CommandType.StoredProcedure;
        Dal.CommandText = "SP_GetAvatarUrl";
        Dal.ParameterCollection = new string[1] { "@UserEmail" };
        if (Session["UserEmail"] != null & Request.Cookies["UserEmail"] == null)
            Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
        if (Session["UserEmail"] != null & Request.Cookies["UserEmail"] != null)
            Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
        if (Session["UserEmail"] == null & Request.Cookies["UserEmail"]==null)
            Dal.ValueCollection = new string[1] { Request.Cookies["UserEmail"].Value };
        DataRow dr = Dal.GetDataRow();
        if (String.IsNullOrEmpty(dr["Image"].ToString()))
        {
            Image5.ImageUrl = ConfigurationManager.AppSettings["DefaultAvatar"];
        }
        else
        {
            Image5.ImageUrl = ConfigurationManager.AppSettings["UserAvatar"] + dr["Image"].ToString();
        }
    }
    protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
    {
        string defaultAddress=" địa điểm...";
        string defaultKeyword=" từ khóa...";
        if (txtKeyword.Text==defaultKeyword)
            txtKeyword.Text="";
        if (txtAddress.Text == defaultAddress)
            txtAddress.Text = "";
        if(txtAddress.Text!="" || txtKeyword.Text!="") 
        Response.Redirect("~/Search.aspx?TenNhaHang=" + txtKeyword.Text + "&DiaChi=" + txtAddress.Text);
    }
    protected bool CheckAdmin()
    {
        bool IsAdmin = false;
        if ((Session["UserEmail"] == null)&& Request.Cookies["UserEmail"]==null)
        {
            IsAdmin = false;
        }
        else
        {
            //try {
            SQLHelper Dal = new SQLHelper();
            Dal.Open();
            Dal.CommandType = CommandType.StoredProcedure;
            Dal.CommandText = "SP_CheckAdmin";
            Dal.ParameterCollection = new string[1] { "@UserEmail" };
            if (Request.Cookies["UserEmail"] !=null)
            {
                if (!String.IsNullOrEmpty(Request.Cookies["UserEmail"].Value))
                {
                    Dal.ValueCollection = new string[1] { Request.Cookies["UserEmail"].ToString() };
                    Dal.ReturnValueParameter = "@ReturnValue";
                    Dal.ExecuteNonQuery();
                    if (Dal.ReturnValue == 0)
                        IsAdmin = false;
                    else
                        IsAdmin = true;
                    Dal.Close();
                    Dal.Dispose();
                }
                else
                {
                    if (Session["UserEmail"] != null)
                    {
                        if (Session["UserEmail"].ToString() != "")
                        {
                            Dal.ValueCollection = new string[1] { Session["UserEmail"].ToString() };
                            Dal.ReturnValueParameter = "@ReturnValue";
                            Dal.ExecuteNonQuery();
                            if (Dal.ReturnValue == 0)
                                IsAdmin = false;
                            else
                                IsAdmin = true;
                            Dal.Close();
                            Dal.Dispose();
                        }
                    }  
                }
            }

            
            //}
            //catch { }
        }
        return IsAdmin;
    }
}
